package hims.view;

import org.primefaces.model.UploadedFile;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.*;

@ManagedBean
@ViewScoped
public class ReportBackupAction implements Serializable {
    private UploadedFile file;

    public void upload() {
        InputStream inputStream = null;
        FileOutputStream fos = null;

        try {
            if (file != null) {
                inputStream = file.getInputstream();
                String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("images/rbk");
                File file1 = new File(path);
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                fos = new FileOutputStream(path + "\\" + file.getFileName());
                int len;
                byte[] buff = new byte[1024];
                while ((len = inputStream.read(buff)) > 0) {
                    fos.write(buff, 0, len);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }
}
